<?php
$admin->checkadminpriv('allowadminleadmail');
!$opration && $opration = 'list';
cpheader();
if ($opration=='toggle_audited'){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	$result = $db->get_one("SELECT audited FROM sdw_leadmails WHERE id=$id");
	$audited = $result['audited']==1 ? 0 : 1;
	$db->query("UPDATE sdw_leadmails SET audited=$audited WHERE id=$id");
	dexit($audited);
}
if ($opration=='save'){
	$_POST['mid'] = isset($_POST['mid']) ? intval($_POST['mid']) : '';
	$_POST['leadid'] = isset($_POST['leadid']) ? intval($_POST['leadid']) : '';
	$_POST['subject'] = isset($_POST['subject']) ? trim($_POST['subject']) : '';
	$_POST['message'] = isset($_POST['message']) ? trim($_POST['message']) : '';
	$db->query("INSERT INTO sdw_leadmails(leadid,mid,subject,message,mailfrom,author,dateline,postip,audited,status)VALUES
	('$_POST[leadid]','$_POST[mid]','$_POST[subject]','$_POST[message]','$adminemail','$_SESSION[admin]','$timestamp','$ip','1','1')");
	$db->query("UPDATE sdw_leadmails SET status=1 WHERE id=".$_POST['mid']);
	$links[0] = array('text'=>$LANG['go_back'],'href'=>$_SERVER['HTTP_REFERER']);
	$links[1] = array('text'=>$LANG['back_list'],'href'=>$_SERVER['PHP_SELF'].'?leadid='.$_POST['leadid']);
	showmsg('save_success',0,$links);
}
if ($opration=='view'){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	$_GET['opration'] = Getpc('opration');
	if ($_GET['opration']=='drop'){
		$_GET['rid'] = isset($_GET['rid']) ? intval($_GET['rid']) : 0;
		$db->query("DELETE FROM sdw_leadmails WHERE id=".$_GET['rid']);
	}
	/*$smarty->assign('mail',$db->get_one("SELECT m.*,r.subject AS rsubject,r.message AS rmessage FROM sdw_department_mails m 
	LEFT JOIN sdw_department_mails r ON m.id=r.mid WHERE m.id=$id"));*/
	$mail = $db->get_one("SELECT * FROM sdw_leadmails WHERE id=$id");
	$mail['message'] = str_replace("\n",'<br>',$mail['message']);
	$smarty->assign('mail',$mail);
	$query = $db->query("SELECT id,leadid,mid,subject,message,author,dateline,postip FROM sdw_leadmails WHERE mid=$id ORDER BY id ASC");
	while ($result = $db->fetch_array($query)){
		$messages[] = $result;
	}
	$smarty->assign('messages',$messages);
}
if ($opration=='drop'){
	$mailid = isset($_GET['id']) ? trim($_GET['id']) : 0;
	$db->query("DELETE FROM sdw_leadmails WHERE (id IN ($mailid)) OR (mid IN ($mailid))");
}
if ($opration=='list' || $inajax){
	$mails = $where = array();
	$pagesize = 20;
	$audited = isset($_GET['audited']) ? intval($_GET['audited']) : -1;
	$status = isset($_GET['status']) ? intval($_GET['status']) : -1;
	if ($audited!=-1)$where[] = "m.audited=$audited";
	if ($status!=-1)$where[] = "m.starus=$status";
	$wheresql = !empty($where) ? ' AND '.implode(' AND ',$where) : ''; 
	$count = $db->get_rows("SELECT m.id FROM sdw_leadmails m LEFT JOIN sdw_leads l ON l.leadid=m.leadid WHERE m.mid=0 $wheresql");
	$pagecount = $count<$pagesize ? 1 : ceil($count/$pagesize);
	$page = $page>$pagecount ? $pagecount : $page;
	$start_limit = ($page-1)*$pagesize;
	$query = $db->query("SELECT m.id,m.leadid,m.subject,m.author,m.postip,m.dateline,m.audited,m.status,l.name,l.title FROM sdw_leadmails m 
	LEFT JOIN sdw_leads l ON l.leadid=m.leadid WHERE m.mid=0 $wheresql ORDER BY m.id DESC LIMIT $start_limit,$pagesize");
	while ($result = $db->fetch_array($query)){
		$result['subject'] = cutstr($result['subject'],40,'...');
		$mails[] = $result;
	}
	$curl = "audited=$audited&status=$status";
	$smarty->assign('curl',$curl);
	$smarty->assign('page',$page);
	$smarty->assign('records',$count);
	$smarty->assign('audited',$audited);
	$smarty->assign('status',$status);
	$smarty->assign('mails',$mails);
	$smarty->assign('pagelink',page_ajax($page,$pagecount,$curl));
}
$smarty->assign('opration',$opration);
$smarty->display('admin_leadmail.htm');
cpfooter();
?>